package net.suncaper.hotel.mapper.impl;

import net.suncaper.hotel.common.utils.JDBCUtils;
import net.suncaper.hotel.mapper.ICustomerMapper;
import net.suncaper.hotel.model.domain.Customer;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 客户数据访问实现类
 */
public class CustomerMapperImpl implements ICustomerMapper {
    private QueryRunner queryRunner = new QueryRunner();

    @Override
    public Customer findById(Integer id) {
        String sql = "SELECT * FROM customer WHERE id = ?";
        try {
            return queryRunner.query(JDBCUtils.getConnection(), sql,
                new BeanHandler<>(Customer.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Customer findByIdNumber(String idType, String idNumber) {
        String sql = "SELECT * FROM customer WHERE id_type = ? AND id_number = ?";
        try {
            return queryRunner.query(JDBCUtils.getConnection(), sql,
                new BeanHandler<>(Customer.class), idType, idNumber);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Customer findByPhone(String phone) {
        String sql = "SELECT * FROM customer WHERE phone = ?";
        try {
            return queryRunner.query(JDBCUtils.getConnection(), sql,
                new BeanHandler<>(Customer.class), phone);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Customer> findAll() {
        String sql = "SELECT * FROM customer ORDER BY create_time DESC";
        try {
            return queryRunner.query(JDBCUtils.getConnection(), sql,
                new BeanListHandler<>(Customer.class));
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int insert(Customer customer) {
        String sql = "INSERT INTO customer (name, id_type, id_number, phone, email, gender, address) " +
                     "VALUES (?, ?, ?, ?, ?, ?, ?)";
        try {
            return queryRunner.update(JDBCUtils.getConnection(), sql,
                customer.getName(), customer.getIdType(), customer.getIdNumber(),
                customer.getPhone(), customer.getEmail(), customer.getGender(),
                customer.getAddress());
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int update(Customer customer) {
        String sql = "UPDATE customer SET name = ?, phone = ?, email = ?, " +
                     "gender = ?, address = ? WHERE id = ?";
        try {
            return queryRunner.update(JDBCUtils.getConnection(), sql,
                customer.getName(), customer.getPhone(), customer.getEmail(),
                customer.getGender(), customer.getAddress(), customer.getId());
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }
}